<?php
# !!!!!!!!!! DON'T MAKE ANY CHANGE IF YOU DON'T KNOW WHAT YOU'RE DOING !!!!!!!!!!
// --------------------------------------------------------------------------------------------------------------
// File Name              :	xml_importer.php
// Last Modification Time	:	2008-02-01 24:00
// Last Modified by       :	turker (turker.biz@gmail.com)
// list of modifications  :
//
//	- 2008-02-01 turker
//		* 24:00 file created.
//
// --------------------------------------------------------------------------------------------------------------

$fileName='orijindb.xml';

/* ------------------------------------------ */

ob_start();

# include required files - start
require_once('inc/config.php');
require_once('inc/classes.php');
require_once('inc/functions.php');
# include required files - end

$mysql=new Mysql(); # connect to mysql server
$valid=new Validation();
$prep=new Prepare();

if (file_exists($fileName)) {
  $xml=simplexml_load_file($fileName);
  if (is_object($xml)) {
    $cats=$xml->kategori;
    $exchanges=$xml->kur;
    $brands=$xml->markalar;
    $items=$xml->urun;
    $prices=$xml->fiyat;

/* ---------------------- KATEGORİLER --------------------------------------- */
    foreach ($cats as $node) {
      $id=(string)$node->id;
      $title=(string)$node->kategori;
      $parent=(string)$node->ust_kategori;
      $order=(string)$node->sira;
      $info=(string)$node->aciklama;
      $date=time()+(60*$TIME_CORRECTION);

      $title=$prep->upper($title);
      $title=$mysql->escape($title);
      $name=makeNicer($title);
      $info=$mysql->escape($info);

      //echo "$title - $info - $name<br />";
      // id, title, info, name, active, parent, order, lang, date
      //$mysql->query("insert into $CATEGORIES_TABLE values('$id','$title','$info','$name','1','$parent','$order','tr','$date')");
    }
    unset($cats);

/* ---------------------- KURLAR --------------------------------------- */
    foreach ($exchanges as $exchange) {
      $id=(string)$exchange->id;
      $exc=(string)$exchange->kur;
      $name=(string)$exchange->aciklama;
      $date=time()+(60*$TIME_CORRECTION);

      $name=$prep->upper($name);
      $name=$mysql->escape($name);
      $exc=$mysql->escape($exc);

      //id, name, exchange, date, active
      //echo "$name - $exc<br />";
      //$mysql->query("insert into $EXCHANGES_TABLE values('$id','$name','$exc','$date','1')");
    }
    unset($exchanges);

/* ---------------------- MARKALAR --------------------------------------- */
    foreach ($brands as $brand) {
      $id=(string)$brand->id;
      $title=(string)$brand->marka;
      $image=(string)$brand->resim;
      $date=time()+(60*$TIME_CORRECTION);

      $name=makeNicer($title);
      $title=$prep->upper($title);
      $image=$mysql->escape($image);
      $title=$mysql->escape($title);

      //id, title, name, image, date, order, active
      //echo "$title - $name - $image<br />";
      //$mysql->query("insert into $BRANDS_TABLE values('$id','$title','$name','$image','$date','0','1')");
    }
    unset($brands);

/* ---------------------- ÜRÜNLER --------------------------------------- */
    $vats=array();
    foreach ($items as $item) {
      $id=(string)$item->id;
      $title=(string)$item->urun;
      $summary=(string)$item->kisa_aciklama;
      $info=(string)$item->aciklama;
      $name=makeNicer($title);
      $home=(string)$item->ana_sayfa;
      $new=(string)$item->yeni;
      $image=(string)$item->resim;
      $date=time()+(60*$TIME_CORRECTION);
      $brand_id=(string)$item->marka;
      $active=(string)$item->aktif;
      $order=(string)$item->sira;
      $stock=(string)$item->stok;
      $cat_id=(string)$item->kategori;
      $vat=(string)$item->kdv;

      $title=$mysql->escape($title);
      $summary=$mysql->escape($summary);
      $info=$mysql->escape($info);
      $image=$mysql->escape($image);
      $vats[$id]=$vat;

      //id, item, summary, info, name, home, new, lang, show_price, xml_code, image, date, brand_id, active, order, stock
      //$mysql->query("insert into $ITEMS_TABLE values('$id','$title','$summary','$info','$name','$home','$new','tr','1','','$image','$date','$brand_id','$active','$order','$stock')");

      //id, item_id, cat_id, primary
      //$mysql->query("insert into $ITEM_CATS_TABLE values('','$id','$cat_id','1')");
    }
    unset($items);

/* ---------------------- FİYATLAR --------------------------------------- */
    foreach ($prices as $price) {
      $id=(string)$price->id;
      $item_id=(string)$price->urun;
      $exchange_id=(string)$price->kur;
      $fiyat=(string)$price->fiyat;
      $info=(string)$price->birim;
      $campaign=(string)$price->kampanya;
      $campaign_price=(string)$price->kampanya_fiyati;
      $active=(string)$price->aktif;
      $vat=$vats[$item_id];
      $date=time()+(60*$TIME_CORRECTION);

      $price=$mysql->escape($price);
      $info=$mysql->escape($info);

      //echo "$id - $active<br />";
      //id, item_id, exchange_id, price, info, campaign, campaign_price, active, vat, t_price, date
      //$mysql->query("insert into $ITEM_PRICES_TABLE values('$id','$item_id','$exchange_id','$fiyat','$info','$campaign','$campaign_price','$active','$vat','','$date')");
    }
  }
  else exit('Not object');
}
else exit('Failed to open '.$fileName);

/*
item_images: id, item_id, image, active, date

<urun>
    +<id>272</id> +
    +<urun>870-C3928 CT ATMAN 302 F İÇ FİLTRE</urun>
    +<kisa_aciklama></kisa_aciklama>
    +<aciklama></aciklama>
    +<kategori>160</kategori>
    +<marka>24</marka>
    +<resim>orijin_296.jpg</resim>
    +<ana_sayfa>0</ana_sayfa>
    <kampanya>0</kampanya>
    <kampanya_aciklama></kampanya_aciklama>
    +<yeni>0</yeni>
    +<sira>18</sira>
    +<aktif>1</aktif>
    +<stok>0</stok>
    <stok_aciklamasi></stok_aciklamasi>
    +<kdv>18</kdv>
</urun>

<fiyat>
  +<id>1</id>
  +<urun>6</urun>
  +<kur>11</kur>
  +<fiyat>100</fiyat>
  +<birim>1 adet</birim>
  +<kampanya>0</kampanya>
  <sira>1</sira>
  +<kampanya_fiyati>0</kampanya_fiyati>
  <goster>0</goster>
  +<aktif>1</aktif>
  <stok>1</stok>
</fiyat>
*/

?>